﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.OracleClient;
using MySql.Data.MySqlClient;
using EnterpriseDataAccess.DataProviders;

namespace EnterpriseDataAccess.Process
{
    sealed class DBEngineFactory
    {
        private DBEngineFactory() { }

        public static DbConnection GetConnection(Server server)
        {
            DbConnection connection = null;
            switch (server)
            {
                case Server.SqlServer:
                    connection = new SqlConnection();
                    break;
                case Server.MySQL:
                    connection = new MySqlConnection();
                    break;
                case Server.Oracle:
                    connection = new OracleConnection();
                    break;
                case Server.OleDb:
                    connection = new OleDbConnection();
                    break;
                case Server.Odbc:
                    connection = new OdbcConnection();
                    break;
                default:
                    return null;
            }
            return connection;
        }

        public static DbCommand GetCommand(Server server)
        {
            switch (server)
            {
                case Server.SqlServer:
                    return new SqlCommand();
                case Server.MySQL:
                    return new MySqlCommand();
                case Server.Oracle:
                    return new OracleCommand();
                case Server.OleDb:
                    return new OleDbCommand();
                case Server.Odbc:
                    return new OdbcCommand();
                default:
                    return null;
            }
        }

        public static DbDataAdapter GetDataAdapter(Server server)
        {
            switch (server)
            {
                case Server.SqlServer:
                    return new SqlDataAdapter();
                case Server.MySQL:
                    return new MySqlDataAdapter();
                case Server.Oracle:
                    return new OracleDataAdapter();
                case Server.OleDb:
                    return new OleDbDataAdapter();
                case Server.Odbc:
                    return new OdbcDataAdapter();
                default:
                    return null;
            }
        }

        public static DbTransaction GetTransaction(DbConnection connection)
        {
            DbTransaction transaction = connection.BeginTransaction();
            return transaction;
        }

        public static DbParameter GetParameter(Server server)
        {
            DbParameter parameter = null;
            switch (server)
            {
                case Server.SqlServer:
                    parameter = new SqlParameter();
                    break;
                case Server.MySQL:
                    parameter = new MySqlParameter();
                    break;
                case Server.Oracle:
                    parameter = new OracleParameter();
                    break;
                case Server.OleDb:
                    parameter = new OleDbParameter();
                    break;
                case Server.Odbc:
                    parameter = new OdbcParameter();
                    break;
                default:
                    return null;
            }
            return parameter;
        }

        public static DbParameter[] GetParameters(Server server, int paramsCount)
        {
            DbParameter[] idbParams = new DbParameter[paramsCount];
            switch (server)
            {
                case Server.SqlServer:
                    for (int i = 0; i < paramsCount;++i)
                       {
                         idbParams[i] = new SqlParameter();
                       }
                    break;
                case Server.MySQL:
                    for (int i = 0; i < paramsCount;++i)
                       {
                           idbParams[i] = new MySqlParameter();
                       }
                    break;
                case Server.Oracle:
                    for (int i = 0; i < paramsCount;++i)
                       {
                           idbParams[i] = new OracleParameter();
                       }
                    break;
                case Server.OleDb:
                    for (int i = 0; i < paramsCount;++i)
                       {
                           idbParams[i] = new OleDbParameter();
                       }
                    break;
                case Server.Odbc:
                    for (int i = 0; i < paramsCount;++i)
                       {
                           idbParams[i] = new OdbcParameter();
                       }
                    break;
                default:
                    idbParams = null;
                    break;
            }

            return idbParams;
        }
    }
}
